home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / COUNTLST.ICN < prev    next >
Text File  |  1992-09-28  |  2KB  |  66 lines

  1. ############################################################################
  2. #
  3. #    File:     countlst.icn
  4. #
  5. #    Subject:  Program to count items in a list
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     December 30, 1991
  10. #
  11. ###########################################################################
  12. #
  13. #     This program counts the number times each line of input occurs and
  14. #  writes a summary.
  15. #
  16. #     With no option, the output is sorted first by decreasing count and within
  17. #  each count, alphabetically. With the option -a, the output is sorted
  18. #  alphabetically.
  19. #
  20. #  The option -t prints a total at the end.
  21. #
  22. ############################################################################
  23. #
  24. #  Links: adlutils, options
  25. #
  26. ############################################################################
  27.  
  28. link adlutils, options
  29.  
  30. procedure main(args)
  31.    local line_count, counter, lines, opts, sort_method, line, total, count
  32.  
  33.    line_count := table(0)        # counts for each line
  34.    counter := table()            # lists of lines for each count
  35.    total := 0                # total number of lines
  36.  
  37.    opts := options(args,"at")
  38.    sort_method := opts["a"]
  39.  
  40.    while line_count[read()] +:= 1 do
  41.       total +:= 1
  42.        
  43.    if \sort_method then {        # alphabetical sort
  44.       line_count := sort(line_count,3)
  45.       while write(get(line_count),"\t",get(line_count))
  46.       }
  47.    else {                 # numerical sort, then alpha
  48.       line_count := sort(line_count,4)
  49.    
  50.       while count := pull(line_count) do {
  51.          /counter[count] := []
  52.          put(counter[count],pull(line_count))
  53.          }
  54.    
  55.       counter := sort(counter,3)
  56.    
  57.       while lines := sort(pull(counter)) do {
  58.          count := pull(counter)
  59.          every write(!lines,"\t",count)
  60.          }
  61.      }
  62.  
  63.    if \opts["t"] then write("\ntotal\t",total)
  64.  
  65. end
  66.